<?php
/**
 * Created by PhpStorm.
 * User: worry
 * Date: 2018/2/8
 * Time: 17:02
 */

namespace common\services;

use common\models\UploadForm;
use Yii;
use yii\web\UploadedFile;

class UploadService extends BasicService
{
    /**
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function save($data)
    {
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            if ($path = $model->upload()) {
                // 文件上传成功
                return $path;
            } else {
                $message = $this->getError($model);
                throw new \Exception($message);
            }
        }
        return false;
    }
}